package org.mm.proxycache.util;

import static com.google.common.base.Objects.nonNull;

import java.net.InetSocketAddress;
import java.util.Date;

import org.apache.mina.common.IoSession;
import org.mm.proxycache.http.HttpRequest;
import org.mm.proxycache.http.HttpResponse;

public class AccessEvent
{
    private final Date timeStamp;
    
    private final AccessType type;
    private final HttpRequest request;
    private final HttpResponse response;
    private final IoSession clientSession;
    private final IoSession serverSession;
    
    private final String clientAddress;
    private final String serverAddress;
    
    public AccessEvent( AccessType type, HttpRequest request, HttpResponse response,
            IoSession clientSession, IoSession serverSession )
    {
        timeStamp = new Date();
        this.type = nonNull( type );
        this.request = nonNull( request );
        this.response = nonNull( response );
        this.clientSession = nonNull( clientSession );
        this.serverSession = serverSession; // Can be null
        
        InetSocketAddress caddr = ((InetSocketAddress) clientSession.getRemoteAddress());
        if ( caddr != null )
            this.clientAddress = caddr.getAddress().getHostAddress();
        else
            this.clientAddress = "NONE";
        
        if ( serverSession != null )
        {
            InetSocketAddress addr = ((InetSocketAddress) serverSession.getRemoteAddress());
            if ( addr != null )
                this.serverAddress = addr.getAddress().getHostAddress();
            else
                this.serverAddress = "NONE";
        }
        else
            this.serverAddress = "NONE";
    }
    
    public Date timeStamp()
    {
        return timeStamp;
    }
    
    /**
     * @return the type
     */
    public AccessType type()
    {
        return type;
    }
    
    /**
     * @return the request
     */
    public HttpRequest request()
    {
        return request;
    }
    
    /**
     * @return the response
     */
    public HttpResponse response()
    {
        return response;
    }
    
    /**
     * @return the clientSession
     */
    public IoSession clientSession()
    {
        return clientSession;
    }
    
    /**
     * @return the serverSession
     */
    public IoSession serverSession()
    {
        return serverSession;
    }
    
    /**
     * @return the clientAddress
     */
    public String clientAddress()
    {
        return clientAddress;
    }
    
    /**
     * @return the serverAddress
     */
    public String serverAddress()
    {
        return serverAddress;
    }
    
}
